package com.qiuzhiyi.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.li.base.BaseActivity;
import com.li.interfaces.OnTopNavigationBarListener;
import com.li.utils.ToastUtil;
import com.qiuzhiyi.App;
import com.qiuzhiyi.activity.LoginActivity;
import com.qiuzhiyi.cache.HumanCache;
import com.qiuzhiyi.item.HumanItem;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description:
 */

public abstract class QZYBaseActivity extends BaseActivity implements OnTopNavigationBarListener, OnRefreshLoadmoreListener {


    protected String mUid;
    protected Context mContext;
    protected Activity mActivity;
    protected int mPage = 0;
    private ProgressDialog mDialog;
    protected boolean mIsRefresh = true;
    private SmartRefreshLayout mSmartRefreshLayout;
    private List<Disposable> mDisposableList;
    protected HumanItem mItem;


    @Override
    protected void initSth() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ButterKnife.bind(this);
        App.addActivity(this);
        mContext = this;
        mActivity = this;
        mItem = HumanCache.getHumanInfo(mContext);
        if (mItem == null) {
            mUid="";
        }else {
            mUid=String.valueOf(mItem.getHumanId());
        }
    }

    @Override
    public void onBackClickListener(View view) {
        finish();
    }

    @Override
    public void onFunctionImgListener(View view) {

    }

    @Override
    public void onFunctionTxtListener(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelAllDisposable();
        if (mSmartRefreshLayout != null) {
            stopRefreshLoad();
        }
        App.removeActivity(this);
    }


    protected void addDisposable(Disposable disposable) {
        if (mDisposableList == null) {
            mDisposableList = new ArrayList<>();
        }
        mDisposableList.add(disposable);
    }

    protected void cancelAllDisposable() {
        if (mDisposableList == null) {
            return;
        }
        for (Disposable disposable : mDisposableList) {
            if (disposable.isDisposed()) {
                continue;
            }
            disposable.dispose();
        }
    }

    protected void setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        mSmartRefreshLayout = smartRefreshLayout;
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mIsRefresh = false;
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mIsRefresh = true;
    }

    protected void stopRefreshLoad() {
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadmore();
    }

    protected void showToast(String content) {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        ToastUtil.showToast(mContext, content);
    }

    protected void start(Class c) {
        Intent intent = new Intent(this, c);
        startActivity(intent);
    }

    protected void startThenFinish(Class c){
        start(c);
        finish();
    }
    protected void startThenFinishWithArg(Class c,Bundle bundle){
        startWithArg(c,bundle);
        finish();
    }

    protected void startWithArg(Class c, Bundle bundle) {
        Intent intent = new Intent(this, c);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void startForResult(Class c, int requestCode) {
        Intent intent = new Intent(this, c);
        startActivityForResult(intent, requestCode);
    }

    protected void startForResultWithArg(Class c, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, c);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    protected void showLoadingDialog(String msg) {
        if (mDialog == null) {
            mDialog = new ProgressDialog(mContext);
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setCancelable(true);
        }
        mDialog.setMessage(msg);
        mDialog.show();
    }

    protected void showLoadingDialog() {
        showLoadingDialog("正在加载");
    }

    protected void dismissLoadingDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();

        }
    }
    protected boolean isEmpty(EditText editText) {
        return editText == null || editText.getText().toString().equals("");
    }
    protected boolean isEmpty(TextView textView) {
        return textView == null || textView.getText().toString().equals("");
    }
    protected boolean isEmpty(String s){
        return TextUtils.isEmpty(s);
    }
    protected void checkIsOnline(){
        mItem = HumanCache.getHumanInfo(mContext);
        if (mItem == null) {
            mUid="";
        }else {
            mUid=String.valueOf(mItem.getHumanId());
        }
        if (TextUtils.isEmpty(mUid)) {
            start(LoginActivity.class);
            return;
        }
    }
}
